package com.souku.finance.application.utils;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.souku.finance.application.db.AvatarUtil;
import com.souku.finance.application.dialog.WaitDialog;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.http.retrofit.RequestService;
import com.souku.finance.application.http.retrofit.RetrofitUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

import cz.msebera.android.httpclient.Header;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/5/16 0016.
 */

public class UploadFile {

    private static WaitDialog waitDialog;

    public static void uploadFile(final Activity activity, String path, String url) {
        try {
            File file = new File(path);
            waitDialog = new WaitDialog(activity);
            if (file.exists() && file.length() > 0) {
                Log.e("fileSize", file.length() + "");
                if (!waitDialog.isShowing()) {
                    waitDialog.show();
                }
                AsyncHttpClient client = new AsyncHttpClient();
                RequestParams params = new RequestParams();
//                params.put("avatar", croppedImageFile, "image/png", "avatar.png");
                params.put("avatar", file);
                params.put(Constant.Token, ShareUtil.getInstance().getString(Constant.Token));
                HashMap<String, String> map = ParamUtil.getParams();
                map.putAll(ParamUtil.getHeaders());
                for (String s : map.keySet()) {
                    params.put(s, map.get(s));
                }
                client.post(UrlConstact.UPLOAD_AVATAR_IMG, params, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int i, Header[] headers, byte[] bytes) {
                        String response = new String(bytes);
                        String avatar = JsonUtil.getString(JsonUtil.getData(response), "avatar");
                        //保存头像url到SQL
                        AvatarUtil.insert(activity, ShareUtil.getInstance().getString(Constant.Mobile), avatar);
                        ShareUtil.getInstance().putExtra(Constant.AvatarUrl, avatar);
                        if (waitDialog.isShowing()) {
                            waitDialog.setfinish("上传成功");
                        }
                    }

                    @Override
                    public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                        Log.e("Throwable", throwable.getMessage());
                        if (waitDialog.isShowing()) {
                            waitDialog.setfinish("上传失败");
                        }
                    }
                });
            } else {
                Toast.makeText(activity, "文件不存在", Toast.LENGTH_LONG).show();
            }

        } catch (FileNotFoundException exception) {
            Toast.makeText(activity, "文件不存在", Toast.LENGTH_LONG).show();
        }
    }

    public static void uploadFile2(final Activity activity, String path) {
        waitDialog = new WaitDialog(activity);

        File file = new File(path);
        if (!waitDialog.isShowing()) {
            waitDialog.show();
        }
        if (file.exists() && file.length() > 0) {
            RequestService requestService = RetrofitUtils.create();
            String oauthToken = ShareUtil.getInstance().getString(Constant.Token);

            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part imageBodyPart = MultipartBody.Part.createFormData("avatar", file.getName(), imageBody);

            Call<ResponseBody> call = requestService.uploadFile(oauthToken, imageBodyPart);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    try {
                        if (response != null) {
                            if (response.body() != null){
                                String body = response.body().string();
                                if (!TextUtils.isEmpty(body)) {
                                    //保存头像url到SQL
                                    String avatar = JsonUtil.getString(JsonUtil.getData(body), "avatar");
                                    AvatarUtil.insert(activity, ShareUtil.getInstance().getString(Constant.Mobile), avatar);
                                    ShareUtil.getInstance().putExtra(Constant.AvatarUrl, avatar);
                                    if (waitDialog.isShowing()) {
                                        waitDialog.setfinish("上传成功");
                                    }
                                    return;
                                }
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (waitDialog.isShowing()) {
                        waitDialog.setfinish("上传失败");
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    if (waitDialog.isShowing()) {
                        waitDialog.setfinish("上传失败");
                    }
                }
            });
        } else {
            Toast.makeText(activity, "裁剪失败", Toast.LENGTH_LONG).show();
        }
    }

}